package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.DishDto;

import com.itheima.entity.Dish;
import com.itheima.entity.Page;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
/**
 * 菜品管理
 */
public class DishController {
    @Autowired(required = false)
    private DishService dishService;


    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
        long employee = (long) session.getAttribute("employee");
        dishDto.setCreateUser(employee);
        dishDto.setUpdateUser(employee);
        //薄唇菜品和口味
        dishService.saveWithFlavor(dishDto);
        return R.success("保存菜品成功");



        }

    @GetMapping("/page")
    public R<Page> page (int page,int pageSize, String name){

        Page<DishDto> dishDtoPage = dishService.page(page, pageSize, name);
        return R.success(dishDtoPage);

    }
    /**
     * 作用：根据id查找菜品和菜品的口味
     * @param id  菜品的id
     * @return
     *
     *
     */
    @GetMapping("/{id}")
    public R<DishDto> get( @PathVariable("id")long id){
      DishDto dishDto=  dishService.findById(id);
      return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
      long empId=(long)  session.getAttribute("employee");
      dishDto.setUpdateUser(empId);
      dishService.updateWithFlavor(dishDto);
      return  R.success("修改成功");

    }

    /**
     * 查找菜品对应数据
     */
    @GetMapping("/list")
    public  R<List<DishDto>> list(long categoryId,Integer status){
      List<DishDto>  dishList= dishService.findByCategoryId(categoryId,status);
      return R.success(dishList);

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.status(status,ids);
        return R.success("修改成功");
    }



















}
